//专业管理store
import { deleteMajor, addMajor, batchDeleteMajor, getMajorList, modifyMajor, queryMajor ,getMajorRequire } from "../../api/major";

const state = {
}

const mutations = {
    NULL:()=>{}
}

const actions = {
    getMajorList({commit}, data) {
        const info={
            collegeId:data.collegeId, //获取管理员ID（需要改动）
            currentPage:data.currentPage,
            rows:data.rows,
            token:data.token,
        };
        return new Promise((resolve, reject) => {
          getMajorList(info).then(data => {
              if(data.code === 1) {     //获取专业信息成功
                  commit('NULL');
                  resolve(data);
              } else {
                  reject(data)
              }
          })
        })
    },
    deleteMajor({commit},data) {
        return new Promise((resolve, reject) => {
            deleteMajor({majorId:data.majorId, token: data.token}).then( data => {
                if(data.code === 1){ // 删除成功
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    deleteBatchMajor({commit},data) {
        return new Promise((resolve, reject) => {
            batchDeleteMajor({majorIds:data.majorIds, token:data.token}).then(data => {
                if(data.code === 1){ // 删除成功
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    queryMajor({commit},data) {
        const info={
            majorName: data.majorName,
            collegeId:data.collegeId,
            currentPage:data.currentPage,
            rows:data.rows,
            token:data.token,
        };
        return new Promise((resolve, reject) => {
            queryMajor(info).then(data => {
                if(data.code === 1){ // 查询成功
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    addMajor({commit},data) {
        return new Promise((resolve, reject) => {
            addMajor({collegeId: data.collegeId,majorName:data.majorRecord.name,token: data.token}).then(data => {
                if(data.code === 1){ // 查询成功
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    modifyMajor({commit},data){
        return new Promise((resolve, reject) => {
            modifyMajor({majorRecord:data.majorRecord,token:data.token})
                .then( data => {
                    if(data.code === 1){ // 查询成功
                        commit('NULL');
                        resolve(data);
                    }else{
                        reject(data)
                    }
                })
        })
    },
    getMajorRequire({commit},data){
        const info={
            majorId: data.majorId,
            token:data.token,
        };
        return new Promise((resolve,reject) => {
            getMajorRequire(info).then(data => {
                console.log(data)
                if(data.code === 1){ // 查询成功
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
